﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.Text;
using ReflectionStudio.Core.Events;
namespace WPFExcelReport
{

    public class WB13QIData : BaseFunction
    {


        public WB13QIData()
            : base()
        {

        }

        public WB13QIData(Dictionary<string, object> DicGlobalPara)
            : base(DicGlobalPara)
        {
        }

        public DataSet BuildData(DataSet setResult, DataRow dwThis)
        {
           // DataSet setReturn = new DataSet();
            DataSet setUI = new DataSet();
            DataSet dsResult = new DataSet();
            string ARTICLE = dwThis["ARTICLE"].ToString().Trim();
            string BATCH = dwThis["BATCH"].ToString().Trim();

            
            try
            { 
                orlReadCommand = OracleDB.CreateCommand();
                orlReadCommand.CommandText = "SELECT TRIM(A.WBBART)||'-'||TRIM(A.WBBFARBE)||'-'||A.WBBVAR AS ARTICLE, "
                    + " A.WBBBESCHICH,E.COLORDES,A.WBBFLZZUGDAT AS WBBEINDAT,A.WBBDPART, "
                    + " COUNT(1) AS PCNUM,SUM(ROUND(WBBISTKLG/1000/0.9144,2)) AS MTR,F.REMARK "
                    + " FROM WBBST A,(SELECT DISTINCT SUBSTR(SDKEY,3,10) as COLOR, "
                    + " SUBSTR(SDDATEN,2,30) AS COLORDES FROM SDDAT "
                    + " WHERE SUBSTR(SDKEY,1,2)='00' AND SDSA = '9998') E, "
                    + " (SELECT DISTINCT SUBSTR(T.TXTISIND,1,12) as STKNR, "
                    + " STRAGG(TRIM(K.TXTKTEXT))OVER(PARTITION BY SUBSTR(T.TXTISIND,1,12)) AS REMARK "
                    + " FROM TXTIND T,TXTKON K "
                    + " WHERE T.TXTIMANDANT = K.TXTKMANDANT AND T.TXTIPIND = K.TXTKPIND "
                    + " AND T.TXTITXTART = 'WBF' ) F "
                    + " WHERE A.WBBBESCHICH = E.COLOR(+) AND A.WBBVERSANDKZ ='N' "
                    + "   AND A.WBBWAKZ = 'F' AND A.WBBISTKLG >0  "
                    + "   AND A.WBBRWKRS||A.WBBRWLFD||A.WBBPOS = F.STKNR(+) "
                    + " AND (TRIM(WBBART) LIKE '" + ARTICLE + "%' OR  '" + ARTICLE + "' ='A')  "
                    + " AND (TRIM(WBBDPART) LIKE '" + BATCH + "%' OR '" + BATCH + "' ='A') "
                    + " GROUP BY A.WBBART,A.WBBFARBE,A.WBBVAR,A.WBBBESCHICH,A.WBBFLZZUGDAT, "
                    + " A.WBBDPART,E.COLORDES,REMARK "; 
                //orlReadCommand.Parameters.Add("ARTICLE", OracleType.NVarChar).Value = dwThis["ARTICLE"].ToString();
                //orlReadCommand.Parameters.Add("WBBDPART", OracleType.NVarChar).Value = dwThis["BATCH"].ToString();
                dsResult = Connect2Proxy(orlReadCommand);
                if (dsResult.Tables.Count > 0)
                {
                    dsResult.Tables[0].TableName = "WB13QI";
                    byte[] b;
                    string value;
                    string ColumnName;
                    foreach (DataRow dw in dsResult.Tables["WB13QI"].Rows)
                    {
                        ColumnName = "WBBBESCHICH";
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dw[ColumnName].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dw[ColumnName] = value;
                        ColumnName = "ARTICLE";
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dw[ColumnName].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dw[ColumnName] = value;
                        ColumnName = "COLORDES";
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dw[ColumnName].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dw[ColumnName] = value;
                        ColumnName = "REMARK";
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dw[ColumnName].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dw[ColumnName] = value;
                    }
                    
                }
                setResult.Merge(dsResult);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return setResult;
        }
    }
}